package storage

import (
	"github.com/pkg/errors"
	"web_framework/model"

	"github.com/jmoiron/sqlx"
)

func CreateDevice(db sqlx.Execer, sn, mac, name string) (int, error) {
	res, err := db.Exec("insert into device(sn,mac,name,create_time) value(?,?,?,NOW())",sn,mac,name)
	if err != nil {
		return 0, err
	}

	ra, err := res.LastInsertId()
	if err != nil {
		return 0, errors.Wrap(err, "get rows affected error")
	}
	if ra == 0 {
		return 0, errors.New("rows not exist")
	}
	return int(ra), nil
}

func BindDeviceUser(db sqlx.Execer, deviceID, uid int64) error {
	_, err := db.Exec("insert into device_user_bind(device_id, uid, bind_time, unbind_time, status) value(?,?,NOW(),NOW(),10)", deviceID, uid)
	if err != nil {
		return err
	}
	return nil
}

func GetDeviceBySnMac(db sqlx.Queryer, sn string, mac string) (model.Router, error) {
	var res model.Router
	err := sqlx.Get(db, &res, "select * from device where sn = ? and mac = ?", sn, mac)
	if err != nil {
		return res, err
	}

	return res, nil
}

func GetDeviceBySn(db sqlx.Queryer, sn string) (model.Router, error) {
	var res model.Router

	err := sqlx.Get(db, &res, "select * from device where sn = ?", sn)
	if err != nil {
		return res, err
	}

	return res, nil
}

func GetDeviceUserBindByUid(db sqlx.Queryer, uid int) ([]model.Router, error) {
	var bindList []model.Router
	err := sqlx.Select(db, &bindList, "SELECT b.`device_id`, b.`name`, b.`sn`, b.`mac` FROM device_user_bind a LEFT JOIN device b ON a.`device_id` = b.`device_id` WHERE a.uid = ? AND a.`status` = 10 AND b.type =1", uid)
	if err != nil {
		return bindList, err
	}
	return bindList, nil
}

func GetDeviceBindBySn(db sqlx.Queryer, sn string) (model.DeviceBind, error) {
	var res model.DeviceBind
	rows, err := db.Queryx("select d.device_id, bind_id, uid, sn, mac, status, d.name from device d left join device_user_bind c on d.`device_id` = c.`device_id` where sn = ?", sn)
	if err != nil {
		return res, err
	}

	if rows.Next() {
		err := rows.StructScan(&res)
		if err != nil {
			return res, err
		}
	}
	return res, nil
}

func GetDeviceBySnDeviceId(db sqlx.Queryer, uid, deviceId int) (model.DeviceUserBind, error) {
	var res model.DeviceUserBind
	err := sqlx.Get(db, &res, "select * from device_user_bind where uid = ? and device_id = ?", uid, deviceId)
	if err != nil {
		return res, err
	}

	return res, nil
}

func UpdateUnbindDevice(db sqlx.Execer, bindId int) error {
	res, err := db.Exec("UPDATE device_user_bind set status = 20, unbind_time = now() WHERE bind_id = ?", bindId)
	if err != nil {
		return err
	}
	ra, err := res.RowsAffected()
	if err != nil {
		return errors.Wrap(err, "get rows affected error")
	}
	if ra == 0 {
		return errors.New("rows not exist")
	}
	return nil
}

func UpdateBindDevice(db sqlx.Execer, bindId, uid int) error {
	res, err := db.Exec("UPDATE device_user_bind set status = 10, uid = ?, unbind_time = now() WHERE bind_id = ?", uid, bindId)
	if err != nil {
		return err
	}
	ra, err := res.RowsAffected()
	if err != nil {
		return errors.Wrap(err, "get rows affected error")
	}
	if ra == 0 {
		return errors.New("rows not exist")
	}
	return nil
}
